home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / ODUtils / Include / Except.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-08  |  5.7 KB  |  208 lines  |  [TEXT/MPS ]

  1.  
  2. #ifndef _EXCEPT_
  3. #define _EXCEPT_
  4.  
  5. #ifndef _ODTYPES_
  6. #include "ODTypes.h"
  7. #endif
  8.  
  9. #ifndef _ERRORDEF_
  10. #include "ErrorDef.xh"    // Clients probably need the error codes as well
  11. #endif
  12.  
  13. #ifndef __SETJMP__
  14. #include <setjmp.h>
  15. #endif
  16.  
  17. #ifdef __LIBRARYMANAGER__
  18. #error "Please don't include both Except.h and LibraryManager.h"
  19. #endif
  20.  
  21. #ifndef FWODEXCE_H
  22. #include "FWODExce.h"
  23. #endif
  24.  
  25. #ifdef _OD_IMPL_
  26. #pragma import on
  27. #endif
  28.  
  29. //=====================================================================================
  30. // ODVolatile
  31. //=====================================================================================
  32.  
  33. // Any variable or parameter that is modified in a TRY block and used in the
  34. // CATCH block must be declared as volatile or it may have an incorrect value
  35. // in the CATCH block.
  36. // Since not all compilers support the 'volatile' keyword, use this instead;
  37.  
  38. #ifndef _NATIVE_EXCEPTIONS_
  39. #define ODVolatile(x)    ((void) &x)
  40. #else
  41. #define ODVolatile(x)    /*no need for this with native exceptions*/
  42. #endif
  43.  
  44. //=====================================================================================
  45. // Exception Handling Macros (native C++ exceptions)
  46. //=====================================================================================
  47.  
  48. #ifdef _NATIVE_EXCEPTIONS_
  49.  
  50. struct ODNativeException {
  51.     ODError error;
  52.     const char *message;
  53. };
  54.  
  55. #define ErrorCode()                (_exception.error)
  56. #define ErrorMessage()            (&_exception.message)
  57.  
  58. #define TRY                                                    \
  59.                 try {
  60.  
  61. #define CATCH_ALL                                            \
  62.                 } catch(ODNativeException _exception) {
  63.  
  64. #define RERAISE                                                \
  65.                 throw
  66.  
  67. #define ENDTRY                                                \
  68.                 }
  69.  
  70. /* CATCH( ) will not work with native exceptions. Don't use it! */
  71.  
  72.  
  73. //=====================================================================================
  74. // Exception Handling Macros (emulated)
  75. //=====================================================================================
  76.  
  77. #else /*not _NATIVE_EXCEPTIONS*/
  78.  
  79. #define ErrorCode()                 (_except.GetODError())
  80. #define ErrorMessage()                (NULL)
  81.  
  82. #define TRY FW_TRY
  83.         
  84. #define CATCH_ALL FW_CATCH_BEGIN FW_CATCH_REFERENCE(FW_XOpenDocException, _except)
  85.         
  86. #define RERAISE FW_THROW_SAME()
  87.  
  88. #define ENDTRY    FW_CATCH_END
  89.  
  90. // CATCH( ) is not compatible with native C++ exceptions.
  91. // Its use is discouraged.
  92. //#define CATCH(e)                                            \
  93. //        } else if (_except.fError==(e)) { 
  94.         
  95.         
  96. #endif /*_NATIVE_EXCEPTIONS*/
  97.  
  98. //=====================================================================================
  99. // Raising Exceptions
  100. //=====================================================================================
  101.  
  102. inline void THROW(ODError error, const char* msg=NULL)
  103.                             {FW_THROW(FW_XOpenDocException(error)); }
  104. inline void THROW_IF_ERROR(ODError error, const char* msg=NULL)
  105.                             {if (error) FW_THROW(FW_XOpenDocException(error)); }
  106. inline void THROW_IF_NULL(void* value)
  107.                             {if (!value) FW_THROW(FW_XOpenDocException(kODErrOutOfMemory)); }
  108. inline void THROW_IF_NULL(void* value, const char* msg)
  109.                             {if (!value) FW_THROW(FW_XOpenDocException(kODErrOutOfMemory)); }
  110. inline void THROW_IF_NULL(void* value, ODError error)
  111.                             {if (!value) FW_THROW(FW_XOpenDocException(error)); }
  112.  
  113. // Optional message parameters:
  114. #define THROW_M(error, m) THROW(error, m)
  115. #define THROW_IF_ERROR_M(error, m) THROW_IF_ERROR(error, m)
  116. #define THROW_IF_NULL_M(value, m) THROW_IF_NULL(value, m)
  117.  
  118. // Call BreakOnThrow(TRUE) to break into the debugger whenever THROW is called.
  119. // (The call returns the previous value of the setting.)
  120. //ODBoolean BreakOnThrow( ODBoolean brk );
  121.  
  122.  
  123. //=====================================================================================
  124. // SOM Exception Utilities
  125. //=====================================================================================
  126.  
  127. // This modified TRY block should be used in SOM methods. It's just like a
  128. // regular TRY...CATCH_ALL...ENDTRY except that the exception code will be
  129. // stored in the Environment. Needless to say you should _not_ reraise!
  130.  
  131. #ifdef _NATIVE_EXCEPTIONS_
  132.  
  133. #define SOM_TRY                                                 \
  134.             TRY
  135.             
  136. #define SOM_CATCH_ALL                                            \
  137.             CATCH_ALL                                            \
  138.             FW_SetODException(ev,ErrorCode());
  139.                 
  140. #define SOM_ENDTRY                                                \
  141.             ENDTRY
  142.             
  143. #else /*not _NATIVE_EXCEPTIONS_*/
  144.  
  145. #define SOM_TRY FW_TRY
  146.     
  147. #define SOM_CATCH_ALL \
  148.             FW_CATCH_BEGIN FW_CATCH_REFERENCE(FW_XOpenDocException, _except) \
  149.             FW_SetODException(ev,ErrorCode());
  150.  
  151. #define SOM_ENDTRY    FW_CATCH_END
  152.  
  153. #endif /*_NATIVE_EXCEPTIONS_*/
  154.  
  155. #if 0
  156. // ODSetSOMException stores an OD error code in the environment.
  157. // ODGetSOMException returns the OD error code (if any) from an environment.
  158.  
  159. void    ODSetSOMException( Environment*, ODError, const char *msg =kODNULL );
  160.  
  161. #ifdef _NATIVE_EXCEPTIONS_
  162. void    ODSetSOMException( Environment*, ODNativeException& );
  163. #endif
  164.  
  165. ODError    ODGetSOMException( Environment *ev );
  166.  
  167. // CHECK_ENV throws an exception if the environment indicates an error.
  168.  
  169. void    CHECK_ENV( Environment* );
  170.  
  171. // SOMCHKEXCEPT is a macro that is called in a .xh file if the ev variable
  172. // indicates an exception is set.
  173. //#define SOMCHKEXCEPT {CHECK_ENV(ev);}
  174. #endif
  175.  
  176.  
  177. //=====================================================================================
  178. // Destructo, a C++ base class for auto-destruct objects
  179. //=====================================================================================
  180.  
  181. class Destructo
  182. {
  183. #ifndef _NATIVE_EXCEPTIONS_
  184. protected:
  185.     Destructo( ) {};    // [HLX] added to satisfy the 
  186.     
  187. public:
  188.     virtual ~Destructo( );
  189.     
  190. #if 0
  191. private:
  192.     Destructo* EmergencyDestruct( );
  193.     Destructo *fPrevDestructo;
  194.     friend class ODExceptionFrame;
  195.  
  196. #endif /*_NATIVE_EXCEPTIONS_*/
  197.     static void* operator new( size_t );    // Make it illegal to allocate on heap
  198.                                             // In future could use alloca??
  199. #endif // 0
  200. };
  201.  
  202. #ifdef _OD_IMPL_
  203. #pragma import off
  204. #endif
  205.  
  206.  
  207. #endif // _EXCEPT_
  208.